using Microsoft.Xna.Framework.Graphics;
using MoreEvent.Common.Systems;
using MoreEvent.Content.Skies;
using ReLogic.Content;
using System.IO;
using Terraria;
using Terraria.Graphics.Effects;
using Terraria.Graphics.Shaders;
using Terraria.ModLoader;

namespace MoreEvent
{
    public class MoreEvent : Mod
    {
        public static Texture2D noise;
        public static Effect auroraEffect;
        public static MoreEvent Instance;
        public override void Load()
        {
            Instance = this;
            if (!Main.dedServ)
            {
                noise = ModContent.Request<Texture2D>("MoreEvent/Assets/Images/Misc/noise", AssetRequestMode.ImmediateLoad).Value;
                auroraEffect = ModContent.Request<Effect>("MoreEvent/Assets/Effects/aurora", AssetRequestMode.ImmediateLoad).Value;
                SkyManager.Instance["MoreEvent:AuroraSky"] = new AuroraSky();
                Filters.Scene["MoreEvent:AuroraSky"] = new Filter(new ScreenShaderData("FilterMiniTower").UseColor(0f, 0f, 0f).UseOpacity(0f), EffectPriority.VeryLow);
                Overlays.Scene["MoreEvent:AuroraSky"] = new AuroraOverlay();
                Filters.Scene["MoreEvent:HeatWave"] = new Filter(new ScreenShaderData("FilterHeatDistortion").UseImage("Images/Misc/noise", 0, null).UseIntensity(4f), EffectPriority.Medium);
                Filters.Scene["MoreEvent:Meteor"] = new Filter(new ScreenShaderData("FilterMiniTower").UseColor(0.5f, 0.5f, 0.5f).UseOpacity(0f), EffectPriority.VeryHigh);
                SkyManager.Instance["MoreEvent:Meteor"] = new MeteorSky();
                Filters.Scene["MoreEvent:MeteorShader"] = new Filter(new ScreenShaderData("FilterMiniTower").UseColor(0f, 0.3f, 0.6f).UseOpacity(0.15f), EffectPriority.High);
            }           
        }
        public override void Unload()
        {
            noise = null;
            auroraEffect = null;
            Instance = null;        
        }
        public override void HandlePacket(BinaryReader reader, int whoAmI)
        {
            byte messageType = reader.ReadByte();
            if (messageType == 0)
            {
                int eventId = reader.ReadInt32();
                ModContent.GetInstance<EventSystem>().StartEvent(eventId);
            }
            else if (messageType == 1)
            {
                int eventId = reader.ReadInt32();
                ModContent.GetInstance<EventSystem>().EndEvent(eventId);
            }
        }
    }
}
